<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>module Gem::GemcutterUtilities - rubygems: Ruby Standard Library Documentation</title>


<script src="../js/navigation.js" defer></script>
<script src="../js/search.js" defer></script>
<script src="../js/search_index.js" defer></script>
<script src="../js/searcher.js" defer></script>
<script src="../js/darkfish.js" defer></script>

<script src="../js/jquery-3.2.0.min.js"></script>

<script src="../js/vue.min.js"></script>
<script src="../js/js.cookie.min.js"></script>

<link href="../css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="../css/rdoc.css" rel="stylesheet">
<link href="../css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "../";
  var index_rel_prefix = "../";
  var darkModeCsseHref = "../css/rdoc-dm.css"
  var defaultModeCssHref = "../css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="module">
  <!-- this is class.html -->

  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0.preview2 NOT FOUND -->
  </div> <!-- end action bar -->

  <div class='wrapper hdiv'>

    


    <nav id='vapp' role="navigation">
    <div id="project-navigation">
      <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="../index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="../table_of_contents.html#pages">Pages</a>
    <a href="../table_of_contents.html#classes">Classes</a>
    <a href="../table_of_contents.html#methods">Methods</a>
  </div>
</div>

      <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

    </div>


    

    <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>
    <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
      <div id="class-metadata">
        
        
        
<div id="includes-section" class="nav-section">
  <h3>Included Modules</h3>

  <ul class="link-list">
    <li><a class="include" href="Text.html">Gem::Text</a>
  </ul>
</div>

        
        
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    <li ><a href="#method-i-add_key_option">#add_key_option</a>
    <li ><a href="#method-i-add_otp_option">#add_otp_option</a>
    <li ><a href="#method-i-api_key">#api_key</a>
    <li ><a href="#method-i-api_key_forbidden-3F">#api_key_forbidden?</a>
    <li ><a href="#method-i-default_host-3F">#default_host?</a>
    <li ><a href="#method-i-fetch_otp">#fetch_otp</a>
    <li ><a href="#method-i-get_key_name">#get_key_name</a>
    <li ><a href="#method-i-get_mfa_params">#get_mfa_params</a>
    <li ><a href="#method-i-get_scope_params">#get_scope_params</a>
    <li ><a href="#method-i-get_user_profile">#get_user_profile</a>
    <li ><a href="#method-i-host">#host</a>
    <li ><a href="#method-i-mfa_unauthorized-3F">#mfa_unauthorized?</a>
    <li ><a href="#method-i-otp">#otp</a>
    <li ><a href="#method-i-pretty_host">#pretty_host</a>
    <li ><a href="#method-i-request_with_otp">#request_with_otp</a>
    <li ><a href="#method-i-rubygems_api_request">#rubygems_api_request</a>
    <li ><a href="#method-i-set_api_key">#set_api_key</a>
    <li ><a href="#method-i-sign_in">#sign_in</a>
    <li ><a href="#method-i-update_scope">#update_scope</a>
    <li ><a href="#method-i-verify_api_key">#verify_api_key</a>
    <li ><a href="#method-i-wait_for_otp_thread">#wait_for_otp_thread</a>
    <li ><a href="#method-i-webauthn_verification_url">#webauthn_verification_url</a>
    <li ><a href="#method-i-with_response">#with_response</a>
  </ul>
</div>

      </div>
     </div>
    </nav>


    <div id='extraz'><div class='adzbox-index'  >
      
     </div>         
    </div>

    <main role="main" aria-labelledby="module-Gem::GemcutterUtilities">
    <h1 id="module-Gem::GemcutterUtilities" class="module">
      module Gem::GemcutterUtilities
    </h1>

    <section class="description">
    
<p>Utility methods for using the RubyGems API.</p>

<p>The <a href="GemcutterUtilities/WebauthnListener.html"><code>WebauthnListener</code></a> class retrieves an OTP after a user successfully WebAuthns with the <a href="../Gem.html"><code>Gem</code></a> host. An instance opens a socket using the TCPServer instance given and listens for a request from the <a href="../Gem.html"><code>Gem</code></a> host. The request should be a GET request to the root path and contains the OTP code in the form of a query parameter ‘code`. The listener will return the code which will be used as the OTP for API requests.</p>

<p>Types of responses sent by the listener after receiving a request:</p>

<pre>- 200 OK: OTP code was successfully retrieved
- 204 No Content: If the request was an OPTIONS request
- 400 Bad Request: If the request did not contain a query parameter `code`
- 404 Not Found: The request was not to the root path
- 405 Method Not Allowed: OTP code was not retrieved because the request was not a GET/OPTIONS request</pre>

<p>Example usage:</p>

<pre class="ruby"><span class="ruby-identifier">thread</span> = <span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">WebauthnListener</span>.<span class="ruby-identifier">listener_thread</span>(<span class="ruby-string">&quot;https://rubygems.example&quot;</span>, <span class="ruby-identifier">server</span>)
<span class="ruby-identifier">thread</span>.<span class="ruby-identifier">join</span>
<span class="ruby-identifier">otp</span> = <span class="ruby-identifier">thread</span>[<span class="ruby-value">:otp</span>]
<span class="ruby-identifier">error</span> = <span class="ruby-identifier">thread</span>[<span class="ruby-value">:error</span>]
</pre>

<p>The <a href="GemcutterUtilities/WebauthnListener.html"><code>WebauthnListener</code></a> Response class is used by the <a href="GemcutterUtilities/WebauthnListener.html"><code>WebauthnListener</code></a> to create responses to be sent to the <a href="../Gem.html"><code>Gem</code></a> host. It creates a Net::HTTPResponse instance when initialized and can be converted to the appropriate format to be sent by a socket using ‘to_s`. Net::HTTPResponse instances cannot be directly sent over a socket.</p>

<p>Types of response classes:</p>

<pre class="ruby"><span class="ruby-operator">-</span> <span class="ruby-constant">OkResponse</span>
<span class="ruby-operator">-</span> <span class="ruby-constant">NoContentResponse</span>
<span class="ruby-operator">-</span> <span class="ruby-constant">BadRequestResponse</span>
<span class="ruby-operator">-</span> <span class="ruby-constant">NotFoundResponse</span>
<span class="ruby-operator">-</span> <span class="ruby-constant">MethodNotAllowedResponse</span>
</pre>

<p>Example usage:</p>

<pre class="ruby"><span class="ruby-identifier">server</span> = <span class="ruby-constant">TCPServer</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">0</span>)
<span class="ruby-identifier">socket</span> = <span class="ruby-identifier">server</span>.<span class="ruby-identifier">accept</span>

<span class="ruby-identifier">response</span> = <span class="ruby-constant">OkResponse</span>.<span class="ruby-identifier">for</span>(<span class="ruby-string">&quot;https://rubygems.example&quot;</span>)
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">print</span> <span class="ruby-identifier">response</span>.<span class="ruby-identifier">to_s</span>
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">close</span>
</pre>

<p>The <a href="GemcutterUtilities/WebauthnPoller.html"><code>WebauthnPoller</code></a> class retrieves an OTP after a user successfully WebAuthns. An instance polls the <a href="../Gem.html"><code>Gem</code></a> host for the OTP code. The polling request (api/v1/webauthn_verification/&lt;webauthn_token&gt;/status.json) is sent to the <a href="../Gem.html"><code>Gem</code></a> host every 5 seconds and will timeout after 5 minutes. If the status field in the json response is “success”, the code field will contain the OTP code.</p>

<p>Example usage:</p>

<pre class="ruby"><span class="ruby-identifier">thread</span> = <span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">WebauthnPoller</span>.<span class="ruby-identifier">poll_thread</span>(
  {},
  <span class="ruby-string">&quot;RubyGems.org&quot;</span>,
  <span class="ruby-string">&quot;https://rubygems.org/api/v1/webauthn_verification/odow34b93t6aPCdY&quot;</span>,
  { <span class="ruby-value">email:</span> <span class="ruby-string">&quot;email@example.com&quot;</span>, <span class="ruby-value">password:</span> <span class="ruby-string">&quot;password&quot;</span> }
)
<span class="ruby-identifier">thread</span>.<span class="ruby-identifier">join</span>
<span class="ruby-identifier">otp</span> = <span class="ruby-identifier">thread</span>[<span class="ruby-value">:otp</span>]
<span class="ruby-identifier">error</span> = <span class="ruby-identifier">thread</span>[<span class="ruby-value">:error</span>]
</pre>

    </section>

      <section id="5Buntitled-5D" class="documentation-section">


      <section class="constants-list">
      <header>
      <h3>Constants</h3>
      </header>
      <dl>
          <dt id="API_SCOPES">API_SCOPES
          <dd>
          <dt id="ERROR_CODE">ERROR_CODE
          <dd>
        </dl>
        </section>

        <section class="attribute-method-details" class="method-section">
        <header>
        <h3>Attributes</h3>
        </header>

          <div id="attribute-i-host" class="method-detail">
            <div class="method-heading attribute-method-heading">
              <span class="method-name">host</span><span
                class="attribute-access-type">[W]</span>
            </div>

            <div class="method-description">
              
              </div>
            </div>
          <div id="attribute-i-scope" class="method-detail">
            <div class="method-heading attribute-method-heading">
              <span class="method-name">scope</span><span
                class="attribute-access-type">[W]</span>
            </div>

            <div class="method-description">
              
              </div>
            </div>
            </section>


                <section id="public-instance-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Instance Methods</h3>
                </header>

                  <div id="method-i-add_key_option" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">add_key_option</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Add the –key option</p>

                              <div class="method-source-code" id="add_key_option-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 23</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">add_key_option</span>
  <span class="ruby-identifier">add_option</span>(<span class="ruby-string">&quot;-k&quot;</span>, <span class="ruby-string">&quot;--key KEYNAME&quot;</span>, <span class="ruby-constant">Symbol</span>,
             <span class="ruby-string">&quot;Use the given API key&quot;</span>,
             <span class="ruby-node">&quot;from #{Gem.configuration.credentials_path}&quot;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">value</span>,<span class="ruby-identifier">options</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">options</span>[<span class="ruby-value">:key</span>] = <span class="ruby-identifier">value</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-add_otp_option" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">add_otp_option</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Add the –otp option</p>

                              <div class="method-source-code" id="add_otp_option-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 34</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">add_otp_option</span>
  <span class="ruby-identifier">add_option</span>(<span class="ruby-string">&quot;--otp CODE&quot;</span>,
             <span class="ruby-string">&quot;Digit code for multifactor authentication&quot;</span>,
             <span class="ruby-string">&quot;You can also use the environment variable GEM_HOST_OTP_CODE&quot;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">value</span>, <span class="ruby-identifier">options</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">options</span>[<span class="ruby-value">:otp</span>] = <span class="ruby-identifier">value</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-api_key" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">api_key</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>The API key from the command options or from the user’s configuration.</p>

                              <div class="method-source-code" id="api_key-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 45</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">api_key</span>
  <span class="ruby-keyword">if</span> <span class="ruby-constant">ENV</span>[<span class="ruby-string">&quot;GEM_HOST_API_KEY&quot;</span>]
    <span class="ruby-constant">ENV</span>[<span class="ruby-string">&quot;GEM_HOST_API_KEY&quot;</span>]
  <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:key</span>]
    <span class="ruby-identifier">verify_api_key</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:key</span>]
  <span class="ruby-keyword">elsif</span> <span class="ruby-constant">Gem</span>.<span class="ruby-identifier">configuration</span>.<span class="ruby-identifier">api_keys</span>.<span class="ruby-identifier">key?</span>(<span class="ruby-identifier">host</span>)
    <span class="ruby-constant">Gem</span>.<span class="ruby-identifier">configuration</span>.<span class="ruby-identifier">api_keys</span>[<span class="ruby-identifier">host</span>]
  <span class="ruby-keyword">else</span>
    <span class="ruby-constant">Gem</span>.<span class="ruby-identifier">configuration</span>.<span class="ruby-identifier">rubygems_api_key</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-host" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">host</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>The host to connect to either from the RUBYGEMS_HOST environment variable or from the user’s configuration</p>

                              <div class="method-source-code" id="host-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 68</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">host</span>
  <span class="ruby-identifier">configured_host</span> = <span class="ruby-constant">Gem</span>.<span class="ruby-identifier">host</span> <span class="ruby-keyword">unless</span>
    <span class="ruby-constant">Gem</span>.<span class="ruby-identifier">configuration</span>.<span class="ruby-identifier">disable_default_gem_server</span>

  <span class="ruby-ivar">@host</span> <span class="ruby-operator">||=</span>
    <span class="ruby-keyword">begin</span>
      <span class="ruby-identifier">env_rubygems_host</span> = <span class="ruby-constant">ENV</span>[<span class="ruby-string">&quot;RUBYGEMS_HOST&quot;</span>]
      <span class="ruby-identifier">env_rubygems_host</span> = <span class="ruby-keyword">nil</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">env_rubygems_host</span>&amp;.<span class="ruby-identifier">empty?</span>

      <span class="ruby-identifier">env_rubygems_host</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">configured_host</span>
    <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-mfa_unauthorized-3F" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">mfa_unauthorized?</span><span
                                class="method-args">(response)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="mfa_unauthorized-3F-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 121</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">mfa_unauthorized?</span>(<span class="ruby-identifier">response</span>)
  <span class="ruby-identifier">response</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Net</span><span class="ruby-operator">::</span><span class="ruby-constant">HTTPUnauthorized</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">response</span>.<span class="ruby-identifier">body</span>.<span class="ruby-identifier">start_with?</span>(<span class="ruby-string">&quot;You have enabled multifactor authentication&quot;</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-otp" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">otp</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>The OTP code from the command options or from the user’s configuration.</p>

                              <div class="method-source-code" id="otp-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 60</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">otp</span>
  <span class="ruby-identifier">options</span>[<span class="ruby-value">:otp</span>] <span class="ruby-operator">||</span> <span class="ruby-constant">ENV</span>[<span class="ruby-string">&quot;GEM_HOST_OTP_CODE&quot;</span>]
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-rubygems_api_request" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">rubygems_api_request</span><span
                                class="method-args">(method, path, host = nil, allowed_push_host = nil, scope: nil, credentials: {}, &amp;block)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Creates an RubyGems API to <code>host</code> and <code>path</code> with the given HTTP <code>method</code>.</p>

<p>If <code>allowed_push_host</code> metadata is present, then it will only allow that host.</p>

                              <div class="method-source-code" id="rubygems_api_request-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 86</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">rubygems_api_request</span>(<span class="ruby-identifier">method</span>, <span class="ruby-identifier">path</span>, <span class="ruby-identifier">host</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">allowed_push_host</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-value">scope:</span> <span class="ruby-keyword">nil</span>, <span class="ruby-value">credentials:</span> {}, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-identifier">require</span> <span class="ruby-string">&quot;net/http&quot;</span>

  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">host</span> = <span class="ruby-identifier">host</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">host</span>
  <span class="ruby-keyword">unless</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">host</span>
    <span class="ruby-identifier">alert_error</span> <span class="ruby-string">&quot;You must specify a gem server&quot;</span>
    <span class="ruby-identifier">terminate_interaction</span>(<span class="ruby-constant">ERROR_CODE</span>)
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">allowed_push_host</span>
    <span class="ruby-identifier">allowed_host_uri</span> = <span class="ruby-constant">URI</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">allowed_push_host</span>)
    <span class="ruby-identifier">host_uri</span>         = <span class="ruby-constant">URI</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">host</span>)

    <span class="ruby-keyword">unless</span> (<span class="ruby-identifier">host_uri</span>.<span class="ruby-identifier">scheme</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">allowed_host_uri</span>.<span class="ruby-identifier">scheme</span>) <span class="ruby-operator">&amp;&amp;</span> (<span class="ruby-identifier">host_uri</span>.<span class="ruby-identifier">host</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">allowed_host_uri</span>.<span class="ruby-identifier">host</span>)
      <span class="ruby-identifier">alert_error</span> <span class="ruby-node">&quot;#{self.host.inspect} is not allowed by the gemspec, which only allows #{allowed_push_host.inspect}&quot;</span>
      <span class="ruby-identifier">terminate_interaction</span>(<span class="ruby-constant">ERROR_CODE</span>)
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">uri</span> = <span class="ruby-constant">URI</span>.<span class="ruby-identifier">parse</span> <span class="ruby-node">&quot;#{self.host}/#{path}&quot;</span>
  <span class="ruby-identifier">response</span> = <span class="ruby-identifier">request_with_otp</span>(<span class="ruby-identifier">method</span>, <span class="ruby-identifier">uri</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">mfa_unauthorized?</span>(<span class="ruby-identifier">response</span>)
    <span class="ruby-identifier">fetch_otp</span>(<span class="ruby-identifier">credentials</span>)
    <span class="ruby-identifier">response</span> = <span class="ruby-identifier">request_with_otp</span>(<span class="ruby-identifier">method</span>, <span class="ruby-identifier">uri</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">api_key_forbidden?</span>(<span class="ruby-identifier">response</span>)
    <span class="ruby-identifier">update_scope</span>(<span class="ruby-identifier">scope</span>)
    <span class="ruby-identifier">request_with_otp</span>(<span class="ruby-identifier">method</span>, <span class="ruby-identifier">uri</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">response</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-set_api_key" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">set_api_key</span><span
                                class="method-args">(host, key)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns true when the user has enabled multifactor authentication from <code>response</code> text and no otp provided by options.</p>

                              <div class="method-source-code" id="set_api_key-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 236</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">set_api_key</span>(<span class="ruby-identifier">host</span>, <span class="ruby-identifier">key</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">default_host?</span>
    <span class="ruby-constant">Gem</span>.<span class="ruby-identifier">configuration</span>.<span class="ruby-identifier">rubygems_api_key</span> = <span class="ruby-identifier">key</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-constant">Gem</span>.<span class="ruby-identifier">configuration</span>.<span class="ruby-identifier">set_api_key</span> <span class="ruby-identifier">host</span>, <span class="ruby-identifier">key</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-sign_in" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">sign_in</span><span
                                class="method-args">(sign_in_host = nil, scope: nil)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Signs in with the RubyGems API at <code>sign_in_host</code> and sets the rubygems API key.</p>

                              <div class="method-source-code" id="sign_in-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 151</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">sign_in</span>(<span class="ruby-identifier">sign_in_host</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-value">scope:</span> <span class="ruby-keyword">nil</span>)
  <span class="ruby-identifier">sign_in_host</span> <span class="ruby-operator">||=</span> <span class="ruby-identifier">host</span>
  <span class="ruby-keyword">return</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">api_key</span>

  <span class="ruby-identifier">pretty_host</span> = <span class="ruby-identifier">pretty_host</span>(<span class="ruby-identifier">sign_in_host</span>)

  <span class="ruby-identifier">say</span> <span class="ruby-node">&quot;Enter your #{pretty_host} credentials.&quot;</span>
  <span class="ruby-identifier">say</span> <span class="ruby-string">&quot;Don&#39;t have an account yet? &quot;</span> \
      <span class="ruby-node">&quot;Create one at #{sign_in_host}/sign_up&quot;</span>

  <span class="ruby-identifier">email</span> = <span class="ruby-identifier">ask</span> <span class="ruby-string">&quot;   Email: &quot;</span>
  <span class="ruby-identifier">password</span> = <span class="ruby-identifier">ask_for_password</span> <span class="ruby-string">&quot;Password: &quot;</span>
  <span class="ruby-identifier">say</span> <span class="ruby-string">&quot;\n&quot;</span>

  <span class="ruby-identifier">key_name</span>     = <span class="ruby-identifier">get_key_name</span>(<span class="ruby-identifier">scope</span>)
  <span class="ruby-identifier">scope_params</span> = <span class="ruby-identifier">get_scope_params</span>(<span class="ruby-identifier">scope</span>)
  <span class="ruby-identifier">profile</span>      = <span class="ruby-identifier">get_user_profile</span>(<span class="ruby-identifier">email</span>, <span class="ruby-identifier">password</span>)
  <span class="ruby-identifier">mfa_params</span>   = <span class="ruby-identifier">get_mfa_params</span>(<span class="ruby-identifier">profile</span>)
  <span class="ruby-identifier">all_params</span>   = <span class="ruby-identifier">scope_params</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">mfa_params</span>)
  <span class="ruby-identifier">warning</span>      = <span class="ruby-identifier">profile</span>[<span class="ruby-string">&quot;warning&quot;</span>]
  <span class="ruby-identifier">credentials</span>  = { <span class="ruby-value">email:</span> <span class="ruby-identifier">email</span>, <span class="ruby-value">password:</span> <span class="ruby-identifier">password</span> }

  <span class="ruby-identifier">say</span> <span class="ruby-node">&quot;#{warning}\n&quot;</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">warning</span>

  <span class="ruby-identifier">response</span> = <span class="ruby-identifier">rubygems_api_request</span>(<span class="ruby-value">:post</span>, <span class="ruby-string">&quot;api/v1/api_key&quot;</span>,
                                  <span class="ruby-identifier">sign_in_host</span>, <span class="ruby-value">credentials:</span> <span class="ruby-identifier">credentials</span>, <span class="ruby-value">scope:</span> <span class="ruby-identifier">scope</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">request</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">request</span>.<span class="ruby-identifier">basic_auth</span> <span class="ruby-identifier">email</span>, <span class="ruby-identifier">password</span>
    <span class="ruby-identifier">request</span>[<span class="ruby-string">&quot;OTP&quot;</span>] = <span class="ruby-identifier">otp</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">otp</span>
    <span class="ruby-identifier">request</span>.<span class="ruby-identifier">body</span> = <span class="ruby-constant">URI</span>.<span class="ruby-identifier">encode_www_form</span>({ <span class="ruby-value">name:</span> <span class="ruby-identifier">key_name</span> }.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">all_params</span>))
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">with_response</span> <span class="ruby-identifier">response</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">resp</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">say</span> <span class="ruby-node">&quot;Signed in with API key: #{key_name}.&quot;</span>
    <span class="ruby-identifier">set_api_key</span> <span class="ruby-identifier">host</span>, <span class="ruby-identifier">resp</span>.<span class="ruby-identifier">body</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-update_scope" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">update_scope</span><span
                                class="method-args">(scope)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="update_scope-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 125</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">update_scope</span>(<span class="ruby-identifier">scope</span>)
  <span class="ruby-identifier">sign_in_host</span>        = <span class="ruby-identifier">host</span>
  <span class="ruby-identifier">pretty_host</span>         = <span class="ruby-identifier">pretty_host</span>(<span class="ruby-identifier">sign_in_host</span>)
  <span class="ruby-identifier">update_scope_params</span> = { <span class="ruby-identifier">scope</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-keyword">true</span> }

  <span class="ruby-identifier">say</span> <span class="ruby-node">&quot;The existing key doesn&#39;t have access of #{scope} on #{pretty_host}. Please sign in to update access.&quot;</span>

  <span class="ruby-identifier">email</span>    = <span class="ruby-identifier">ask</span> <span class="ruby-string">&quot;   Email: &quot;</span>
  <span class="ruby-identifier">password</span> = <span class="ruby-identifier">ask_for_password</span> <span class="ruby-string">&quot;Password: &quot;</span>

  <span class="ruby-identifier">response</span> = <span class="ruby-identifier">rubygems_api_request</span>(<span class="ruby-value">:put</span>, <span class="ruby-string">&quot;api/v1/api_key&quot;</span>,
                                  <span class="ruby-identifier">sign_in_host</span>, <span class="ruby-value">scope:</span> <span class="ruby-identifier">scope</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">request</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">request</span>.<span class="ruby-identifier">basic_auth</span> <span class="ruby-identifier">email</span>, <span class="ruby-identifier">password</span>
    <span class="ruby-identifier">request</span>[<span class="ruby-string">&quot;OTP&quot;</span>] = <span class="ruby-identifier">otp</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">otp</span>
    <span class="ruby-identifier">request</span>.<span class="ruby-identifier">body</span> = <span class="ruby-constant">URI</span>.<span class="ruby-identifier">encode_www_form</span>({ <span class="ruby-value">:api_key</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-identifier">api_key</span> }.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">update_scope_params</span>))
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">with_response</span> <span class="ruby-identifier">response</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">_resp</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">say</span> <span class="ruby-node">&quot;Added #{scope} scope to the existing API key&quot;</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-verify_api_key" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">verify_api_key</span><span
                                class="method-args">(key)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Retrieves the pre-configured API key <code>key</code> or terminates interaction with an error.</p>

                              <div class="method-source-code" id="verify_api_key-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 192</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">verify_api_key</span>(<span class="ruby-identifier">key</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">Gem</span>.<span class="ruby-identifier">configuration</span>.<span class="ruby-identifier">api_keys</span>.<span class="ruby-identifier">key?</span> <span class="ruby-identifier">key</span>
    <span class="ruby-constant">Gem</span>.<span class="ruby-identifier">configuration</span>.<span class="ruby-identifier">api_keys</span>[<span class="ruby-identifier">key</span>]
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">alert_error</span> <span class="ruby-string">&quot;No such API key. Please add it to your configuration (done automatically on initial `gem push`).&quot;</span>
    <span class="ruby-identifier">terminate_interaction</span>(<span class="ruby-constant">ERROR_CODE</span>)
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-with_response" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">with_response</span><span
                                class="method-args">(response, error_prefix = nil) { |response| ... }</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>If <code>response</code> is an HTTP Success (2XX) response, yields the response if a block was given or shows the response body to the user.</p>

<p>If the response was not successful, shows an error to the user including the <code>error_prefix</code> and the response body. If the response was a permanent redirect, shows an error to the user including the redirect location.</p>

                              <div class="method-source-code" id="with_response-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 209</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">with_response</span>(<span class="ruby-identifier">response</span>, <span class="ruby-identifier">error_prefix</span> = <span class="ruby-keyword">nil</span>)
  <span class="ruby-keyword">case</span> <span class="ruby-identifier">response</span>
  <span class="ruby-keyword">when</span> <span class="ruby-constant">Net</span><span class="ruby-operator">::</span><span class="ruby-constant">HTTPSuccess</span> <span class="ruby-keyword">then</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span>
      <span class="ruby-keyword">yield</span> <span class="ruby-identifier">response</span>
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">say</span> <span class="ruby-identifier">clean_text</span>(<span class="ruby-identifier">response</span>.<span class="ruby-identifier">body</span>)
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">when</span> <span class="ruby-constant">Net</span><span class="ruby-operator">::</span><span class="ruby-constant">HTTPPermanentRedirect</span>, <span class="ruby-constant">Net</span><span class="ruby-operator">::</span><span class="ruby-constant">HTTPRedirection</span> <span class="ruby-keyword">then</span>
    <span class="ruby-identifier">message</span> = <span class="ruby-node">&quot;The request has redirected permanently to #{response[&quot;location&quot;]}. Please check your defined push host URL.&quot;</span>
    <span class="ruby-identifier">message</span> = <span class="ruby-node">&quot;#{error_prefix}: #{message}&quot;</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">error_prefix</span>

    <span class="ruby-identifier">say</span> <span class="ruby-identifier">clean_text</span>(<span class="ruby-identifier">message</span>)
    <span class="ruby-identifier">terminate_interaction</span>(<span class="ruby-constant">ERROR_CODE</span>)
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">message</span> = <span class="ruby-identifier">response</span>.<span class="ruby-identifier">body</span>
    <span class="ruby-identifier">message</span> = <span class="ruby-node">&quot;#{error_prefix}: #{message}&quot;</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">error_prefix</span>

    <span class="ruby-identifier">say</span> <span class="ruby-identifier">clean_text</span>(<span class="ruby-identifier">message</span>)
    <span class="ruby-identifier">terminate_interaction</span>(<span class="ruby-constant">ERROR_CODE</span>)
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                          </section>

                <section id="private-instance-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Private Instance Methods</h3>
                </header>

                  <div id="method-i-api_key_forbidden-3F" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">api_key_forbidden?</span><span
                                class="method-args">(response)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="api_key_forbidden-3F-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 368</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">api_key_forbidden?</span>(<span class="ruby-identifier">response</span>)
  <span class="ruby-identifier">response</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Net</span><span class="ruby-operator">::</span><span class="ruby-constant">HTTPForbidden</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">response</span>.<span class="ruby-identifier">body</span>.<span class="ruby-identifier">start_with?</span>(<span class="ruby-string">&quot;The API key doesn&#39;t have access&quot;</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-default_host-3F" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">default_host?</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="default_host-3F-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 328</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">default_host?</span>
  <span class="ruby-identifier">host</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_HOST</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-fetch_otp" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">fetch_otp</span><span
                                class="method-args">(credentials)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="fetch_otp-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 255</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">fetch_otp</span>(<span class="ruby-identifier">credentials</span>)
  <span class="ruby-identifier">options</span>[<span class="ruby-value">:otp</span>] = <span class="ruby-keyword">if</span> <span class="ruby-identifier">webauthn_url</span> = <span class="ruby-identifier">webauthn_verification_url</span>(<span class="ruby-identifier">credentials</span>)
    <span class="ruby-identifier">server</span> = <span class="ruby-constant">TCPServer</span>.<span class="ruby-identifier">new</span> <span class="ruby-value">0</span>
    <span class="ruby-identifier">port</span> = <span class="ruby-identifier">server</span>.<span class="ruby-identifier">addr</span>[<span class="ruby-value">1</span>].<span class="ruby-identifier">to_s</span>

    <span class="ruby-identifier">url_with_port</span> = <span class="ruby-node">&quot;#{webauthn_url}?port=#{port}&quot;</span>
    <span class="ruby-identifier">say</span> <span class="ruby-node">&quot;You have enabled multi-factor authentication. Please visit #{url_with_port} to authenticate via security device. If you can&#39;t verify using WebAuthn but have OTP enabled, you can re-run the gem signin command with the `--otp [your_code]` option.&quot;</span>

    <span class="ruby-identifier">threads</span> = [<span class="ruby-constant">WebauthnListener</span>.<span class="ruby-identifier">listener_thread</span>(<span class="ruby-identifier">host</span>, <span class="ruby-identifier">server</span>), <span class="ruby-constant">WebauthnPoller</span>.<span class="ruby-identifier">poll_thread</span>(<span class="ruby-identifier">options</span>, <span class="ruby-identifier">host</span>, <span class="ruby-identifier">webauthn_url</span>, <span class="ruby-identifier">credentials</span>)]
    <span class="ruby-identifier">otp_thread</span> = <span class="ruby-identifier">wait_for_otp_thread</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">threads</span>)

    <span class="ruby-identifier">threads</span>.<span class="ruby-identifier">each</span>(<span class="ruby-operator">&amp;</span><span class="ruby-value">:join</span>)

    <span class="ruby-keyword">if</span> <span class="ruby-identifier">error</span> = <span class="ruby-identifier">otp_thread</span>[<span class="ruby-value">:error</span>]
      <span class="ruby-identifier">alert_error</span> <span class="ruby-identifier">error</span>.<span class="ruby-identifier">message</span>
      <span class="ruby-identifier">terminate_interaction</span>(<span class="ruby-value">1</span>)
    <span class="ruby-keyword">end</span>

    <span class="ruby-identifier">say</span> <span class="ruby-string">&quot;You are verified with a security device. You may close the browser window.&quot;</span>
    <span class="ruby-identifier">otp_thread</span>[<span class="ruby-value">:otp</span>]
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">say</span> <span class="ruby-string">&quot;You have enabled multi-factor authentication. Please enter OTP code.&quot;</span>
    <span class="ruby-identifier">ask</span> <span class="ruby-string">&quot;Code: &quot;</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-get_key_name" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">get_key_name</span><span
                                class="method-args">(scope)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="get_key_name-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 354</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">get_key_name</span>(<span class="ruby-identifier">scope</span>)
  <span class="ruby-identifier">hostname</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">gethostname</span> <span class="ruby-operator">||</span> <span class="ruby-string">&quot;unknown-host&quot;</span>
  <span class="ruby-identifier">user</span> = <span class="ruby-constant">ENV</span>[<span class="ruby-string">&quot;USER&quot;</span>] <span class="ruby-operator">||</span> <span class="ruby-constant">ENV</span>[<span class="ruby-string">&quot;USERNAME&quot;</span>] <span class="ruby-operator">||</span> <span class="ruby-string">&quot;unknown-user&quot;</span>
  <span class="ruby-identifier">ts</span> = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>.<span class="ruby-identifier">strftime</span>(<span class="ruby-string">&quot;%Y%m%d%H%M%S&quot;</span>)
  <span class="ruby-identifier">default_key_name</span> = <span class="ruby-node">&quot;#{hostname}-#{user}-#{ts}&quot;</span>

  <span class="ruby-identifier">key_name</span> = <span class="ruby-identifier">ask</span> <span class="ruby-node">&quot;API Key name [#{default_key_name}]: &quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">scope</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">key_name</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">key_name</span>.<span class="ruby-identifier">empty?</span>
    <span class="ruby-identifier">default_key_name</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">key_name</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-get_mfa_params" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">get_mfa_params</span><span
                                class="method-args">(profile)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="get_mfa_params-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 344</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">get_mfa_params</span>(<span class="ruby-identifier">profile</span>)
  <span class="ruby-identifier">mfa_level</span> = <span class="ruby-identifier">profile</span>[<span class="ruby-string">&quot;mfa&quot;</span>]
  <span class="ruby-identifier">params</span> = {}
  <span class="ruby-keyword">if</span> [<span class="ruby-string">&quot;ui_only&quot;</span>, <span class="ruby-string">&quot;ui_and_gem_signin&quot;</span>].<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">mfa_level</span>)
    <span class="ruby-identifier">selected</span> = <span class="ruby-identifier">ask_yes_no</span>(<span class="ruby-string">&quot;Would you like to enable MFA for this key? (strongly recommended)&quot;</span>)
    <span class="ruby-identifier">params</span>[<span class="ruby-string">&quot;mfa&quot;</span>] = <span class="ruby-keyword">true</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">selected</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">params</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-get_scope_params" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">get_scope_params</span><span
                                class="method-args">(scope)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="get_scope_params-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 311</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">get_scope_params</span>(<span class="ruby-identifier">scope</span>)
  <span class="ruby-identifier">scope_params</span> = {}

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">scope</span>
    <span class="ruby-identifier">scope_params</span> = { <span class="ruby-identifier">scope</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-keyword">true</span> }
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">say</span> <span class="ruby-string">&quot;Please select scopes you want to enable for the API key (y/n)&quot;</span>
    <span class="ruby-constant">API_SCOPES</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">s</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">selected</span> = <span class="ruby-identifier">ask_yes_no</span>(<span class="ruby-identifier">s</span>.<span class="ruby-identifier">to_s</span>, <span class="ruby-keyword">false</span>)
      <span class="ruby-identifier">scope_params</span>[<span class="ruby-identifier">s</span>] = <span class="ruby-keyword">true</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">selected</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">say</span> <span class="ruby-string">&quot;\n&quot;</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">scope_params</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-get_user_profile" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">get_user_profile</span><span
                                class="method-args">(email, password)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="get_user_profile-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 332</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">get_user_profile</span>(<span class="ruby-identifier">email</span>, <span class="ruby-identifier">password</span>)
  <span class="ruby-keyword">return</span> {} <span class="ruby-keyword">unless</span> <span class="ruby-identifier">default_host?</span>

  <span class="ruby-identifier">response</span> = <span class="ruby-identifier">rubygems_api_request</span>(<span class="ruby-value">:get</span>, <span class="ruby-string">&quot;api/v1/profile/me.yaml&quot;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">request</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">request</span>.<span class="ruby-identifier">basic_auth</span> <span class="ruby-identifier">email</span>, <span class="ruby-identifier">password</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">with_response</span> <span class="ruby-identifier">response</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">resp</span><span class="ruby-operator">|</span>
    <span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">ConfigFile</span>.<span class="ruby-identifier">load_with_rubygems_config_hash</span>(<span class="ruby-identifier">clean_text</span>(<span class="ruby-identifier">resp</span>.<span class="ruby-identifier">body</span>))
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-pretty_host" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">pretty_host</span><span
                                class="method-args">(host)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="pretty_host-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 303</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">pretty_host</span>(<span class="ruby-identifier">host</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">default_host?</span>
    <span class="ruby-string">&quot;RubyGems.org&quot;</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">host</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-request_with_otp" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">request_with_otp</span><span
                                class="method-args">(method, uri, &amp;block)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="request_with_otp-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 246</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">request_with_otp</span>(<span class="ruby-identifier">method</span>, <span class="ruby-identifier">uri</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-identifier">request_method</span> = <span class="ruby-constant">Net</span><span class="ruby-operator">::</span><span class="ruby-constant">HTTP</span>.<span class="ruby-identifier">const_get</span> <span class="ruby-identifier">method</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">capitalize</span>

  <span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">RemoteFetcher</span>.<span class="ruby-identifier">fetcher</span>.<span class="ruby-identifier">request</span>(<span class="ruby-identifier">uri</span>, <span class="ruby-identifier">request_method</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">req</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">req</span>[<span class="ruby-string">&quot;OTP&quot;</span>] = <span class="ruby-identifier">otp</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">otp</span>
    <span class="ruby-identifier">block</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">req</span>)
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-wait_for_otp_thread" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">wait_for_otp_thread</span><span
                                class="method-args">(*threads)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="wait_for_otp_thread-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 281</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">wait_for_otp_thread</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">threads</span>)
  <span class="ruby-identifier">loop</span> <span class="ruby-keyword">do</span>
    <span class="ruby-identifier">threads</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">otp_thread</span><span class="ruby-operator">|</span>
      <span class="ruby-keyword">return</span> <span class="ruby-identifier">otp_thread</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">otp_thread</span>.<span class="ruby-identifier">alive?</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">sleep</span> <span class="ruby-value">0.1</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">ensure</span>
  <span class="ruby-identifier">threads</span>.<span class="ruby-identifier">each</span>(<span class="ruby-operator">&amp;</span><span class="ruby-value">:exit</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-webauthn_verification_url" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">webauthn_verification_url</span><span
                                class="method-args">(credentials)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="webauthn_verification_url-source">
            <pre><span class="ruby-comment"># File rubygems/gemcutter_utilities.rb, line 292</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">webauthn_verification_url</span>(<span class="ruby-identifier">credentials</span>)
  <span class="ruby-identifier">response</span> = <span class="ruby-identifier">rubygems_api_request</span>(<span class="ruby-value">:post</span>, <span class="ruby-string">&quot;api/v1/webauthn_verification&quot;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">request</span><span class="ruby-operator">|</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">credentials</span>.<span class="ruby-identifier">empty?</span>
      <span class="ruby-identifier">request</span>.<span class="ruby-identifier">add_field</span> <span class="ruby-string">&quot;Authorization&quot;</span>, <span class="ruby-identifier">api_key</span>
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">request</span>.<span class="ruby-identifier">basic_auth</span> <span class="ruby-identifier">credentials</span>[<span class="ruby-value">:email</span>], <span class="ruby-identifier">credentials</span>[<span class="ruby-value">:password</span>]
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">response</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Net</span><span class="ruby-operator">::</span><span class="ruby-constant">HTTPSuccess</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">response</span>.<span class="ruby-identifier">body</span> <span class="ruby-operator">:</span> <span class="ruby-keyword">nil</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                          </section>

              </section>
              </main>



            </div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is provided by <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>. </p><p><a href="https://jamesbritt.bandcamp.com/">Hack your world.  Feed your head.  Live curious</a>.</p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

